package com.team.arms.xitong.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.team.arms.common.Result;
import com.team.arms.common.ry.core.domain.AjaxResult;
import com.team.arms.common.util.ConvertUtil;
import com.team.arms.utils.MDPasswordUtils;
import com.team.arms.utils.PageUtil;
import com.team.arms.xitong.entity.User;
import com.team.arms.xitong.entity.dto.UserUpdateDto;
import com.team.arms.xitong.entity.vo.UserVo;
import com.team.arms.xitong.mapper.UserMapper;
import com.team.arms.xitong.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Huazige
 * @since 2022-06-24
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
@Api(tags = "系统管理-用户管理")
public class UserController {

    @Autowired
    UserMapper userMapper;
    @Autowired
    IUserService iUserService;

    @Autowired
    private ConvertUtil convertUtil;

    @ApiOperation("获取所有用户")
    @GetMapping(value = "/all")
    public List<UserVo> GetUserAllMsg() {

        List<User> users = userMapper.selectList(null);
        List<UserVo>  userVoList=  convertUtil.convert(users, UserVo.class);

        /*用了两次查询可能效率会低一点，后续更改可以考虑连表的方式*/
        return  userVoList;
    }
   /* @ApiOperation("获取所有用户")
    @GetMapping(value = "/all")
    public List<UserVo> GetUserAllMsg() {

        List<User> users = userMapper.selectList(null);
        List<UserVo>  userVoList=  convertUtil.convert(users, UserVo.class);

        *//*用了两次查询可能效率会低一点，后续更改可以考虑连表的方式*//*
        return  userVoList;
    }*/
    @ApiOperation("分页显示用户")
    @GetMapping(value = "/list/{pageNum}/{pageSize}")
    public Result selectMsg(
            @ApiParam("当前页数")       @PathVariable("pageNum")Integer pageNum,
            @ApiParam("每页的数据量")       @PathVariable("pageSize")Integer pageSize

    ){
        List<User> users = userMapper.selectList(null);
        List<UserVo>  userVoList=  convertUtil.convert(users, UserVo.class);

        //转换效率的问题,直接sql查询返回这个类型效率相对高一些
        Page page = PageUtil.list2Page(userVoList, pageNum, pageSize);
        return page !=null ? Result.success("分页成功").data(page): Result.error("分页失败");
    }

    @ApiOperation(value = "用户删除+批量删除")
    @DeleteMapping("/deleteList")
    public Result deletelist(@RequestParam("idList") List idList){
        boolean b = iUserService.removeByIds(idList);
        return b ? Result.success("批量删除成功") : Result.error("批量删除失败");
    }

    @ApiOperation("用户登录")
//    @GetMapping("/register")
//    public Result register(@Validated @RequestBody User user){
    @PostMapping(value = "/login/{username}/{password}")
    public Result login  (
            @ApiParam(name = "username", value = "账号", required = true)
            @PathVariable(value="username")  String username,

            @ApiParam(name = "password", value = "密码", required = true)
            @PathVariable(value="password") String password
    ){
       /* BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(10);
        String encodePasswod = encoder.encode(password);*/
        QueryWrapper<User> wrapper = new QueryWrapper();
        wrapper.eq("username",username)
                .eq("password", MDPasswordUtils.md5Encrypt(password));
        User user = userMapper.selectOne(wrapper);
        UserVo userVo=  convertUtil.convert(user, UserVo.class);

        if(userVo!=null){
            return Result.success("登录成功").data(userVo);
        }
        else{
            return Result.error("登录异常");

        }
    }
    //普通用户注册
   /* @ApiOperation("用户增加")
//    @GetMapping("/register")
//    public Result register(@Validated @RequestBody User user){
    @PostMapping(value = "/register/{id}/{name}/{username}/{password}")
    public Result register  (

            @ApiParam(name = "username", value = "账号", required = true)
            @PathVariable(value="username")  String username,

            @ApiParam(name = "password", value = "密码", required = true)
            @PathVariable(value="password") String password
    ){
        User user = new User();

        user.setUsername(username);
        *//*获取这个注册账号的对应的年份*//*
        String encodePasswod = MDPasswordUtils.md5Encrypt(password);
        user.setPassword(encodePasswod);
        QueryWrapper<User> wrapper = new QueryWrapper();
        wrapper.eq("username",username).eq("password",MDPasswordUtils.md5Encrypt(password));
        User user1 = userMapper.selectOne(wrapper);
        if(user1!=null){
            return Result.error("注册失败,该用户已存在");
        }

        userMapper.insert(user);
        *//* System.out.println(2);*//*
        return Result.success("注册成功").data(user);
    }*/
    @ApiOperation("修改用户信息")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody UserUpdateDto userUpdateDto){
        String userId = userUpdateDto.getUserId();
        QueryWrapper<User> wrapper = new QueryWrapper();
        wrapper.eq("user_id",userId);
        User user = convertUtil.convert(userUpdateDto, User.class);
        boolean b =     iUserService.update(user, wrapper);//前新后旧
        return b ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
    }

    @ApiOperation("用户注册(管理员)")
//    @GetMapping("/register")
//    public Result register(@Validated @RequestBody User user){
    @PostMapping(value = "/register/{id}/{username}/{password}")
    public Result register  (
            @ApiParam(name = "id", value = "编号", required = true)
            @PathVariable(value="id")  String id,


            @ApiParam(name = "username", value = "账号", required = true)
            @PathVariable(value="username")  String username,

            @ApiParam(name = "password", value = "密码", required = true)
            @PathVariable(value="password") String password
    ){
       /* BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(10);
        String encodePasswod = encoder.encode(password);*/
//       userMapper.se
        System.out.println(1);
        User user2 = userMapper.selectById(id);
        System.out.println(2);
        if (user2.getRoleId().equals("1")){
            User user = new User();

            user.setUsername(username);
            /*获取这个注册账号的对应的年份*/
            String encodePasswod = MDPasswordUtils.md5Encrypt(password);
            user.setPassword(encodePasswod);
            QueryWrapper<User> wrapper = new QueryWrapper();
            wrapper.eq("username",username).eq("password",MDPasswordUtils.md5Encrypt(password));
            User user1 = userMapper.selectOne(wrapper);
            if(user1!=null){
                return Result.error("注册失败,该用户已存在");
            }

            userMapper.insert(user);
            /* System.out.println(2);*/
            return Result.success("注册成功").data(user);
        }

        else {
            return Result.error("没有权限注册");

        }
    }

    @ApiOperation("密码修改")
//    @GetMapping("/register")
//    public Result register(@Validated @RequestBody User user){
    @PostMapping(value = "/alter/{id}/{oldpassword}/{newpassword}")
    public Result alterpsw (
            @ApiParam(name = "id", value = "id号", required = true)
            @PathVariable(value="id")  String id,

            @ApiParam(name = "oldpassword", value = "旧密码", required = true)
            @PathVariable(value="oldpassword") String oldpassword ,

            @ApiParam(name = "newpassword", value = "新密码", required = true)
            @PathVariable(value="newpassword") String newpassword
    ){
       /* BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(10);
        String encodePasswod = encoder.encode(password);*/
        QueryWrapper<User> wrapper = new QueryWrapper();
        System.out.println(id);
        System.out.println(MDPasswordUtils.md5Encrypt(oldpassword));
        wrapper.eq("user_id",id).eq("password", MDPasswordUtils.md5Encrypt(oldpassword));

        User user = userMapper.selectOne(wrapper);
        System.out.println(user.getRoleId());
        if(user!=null){

            user.setPassword(MDPasswordUtils.md5Encrypt(newpassword));
            QueryWrapper<User> wrapper2 = new QueryWrapper();
            wrapper2.eq("user_id",id);
            userMapper.update(user,wrapper2);
            return Result.success("密码修改成功");
        }
        else{
            return Result.error("密码修改失败,用户密码输入错误");

        }
    }



}

